var app = getApp();
var db = wx.cloud.database()
 const _ = db.command
var util = require('../../utils/util.js')
Page({

  data: {
    address: {},
    cartList: [],
    totalPrice: 0,
    totalNum: 0,
    hasAddress:false,
    cart:[]
  },
  onLoad:function(options) {
    var cartList = JSON.parse(options.cart)
    console.log(cartList);
    this.setData({
      cartList
    })
  },
  onShow: function () {
    this.getUserAddress()
    this.getCartStatus()
  },
  //获取用户地址
  getUserAddress() {
    let that=this
    let {address} =this.data
    address= wx.getStorage({
      key: 'userAddress',
      success:function(res) {
        console.log(res);
        address=res.data
        if(address.length!==0) {
          let dizhi=address.provinceName+address.cityName+address.countyName+address.detailInfo
          that.setData({
            address,
            hasAddress:true,
            dizhi
          })
        } 
      }
    })

  },
  //添加用户地址
  addUserAddress() {
    let {address} =this.data
    wx.chooseAddress({
      success: (res) => {
         address=res
          wx.setStorageSync('userAddress', address)
          let dizhi=address.provinceName+address.cityName+address.countyName+address.detailInfo
         this.setData({
           address,
           hasAddress:true,
           dizhi
         })
      }
    })
  },
  //修改用户地址
  updataAddress() {
    let that=this
    wx.chooseAddress({
      success: (res) => {
        let userAddress= res
        let address=res
        let dizhi=address.provinceName+address.cityName+address.countyName+address.detailInfo
         wx.setStorageSync('userAddress', userAddress)
         that.setData({
           address,dizhi
         })
         console.log(that.data.address);
      },
    })
  },
 //获取购物车情况
 getCartStatus() {
   let {cartList,totalPrice,totalNum,cart} =this.data
   cart=[]
   totalPrice=0
   totalNum=0
   cartList.forEach(v=>{
     if(v.checked) {
      totalPrice += v.goods_price * v.num
      totalNum += v.num
      cart=cart.concat(v)
     }
   })
   console.log(cart);
   this.setData({
    totalPrice,totalNum,cart
   })
 },
 //支付并生成订单号
 orderPay() {
   let that=this
   let {cart} =this.data
   wx.showToast({
     title: '支付成功',
     icon:'success',
     success:function() {
       let ordernumber = ''
       let ordernumberLength=16
       var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
       for (var i =0;i<ordernumberLength;i++) {
        var index = Math.floor(Math.random() * 36);
        ordernumber += random[index];
       }
      //  console.log(ordernumber);
      let orderPrice= that.data.totalPrice
      var dayTime = util.formatTime(new Date());
      // console.log(orderPrice);
      // console.log(dayTime);
      db.collection('orderList').add({
        data:{
          ordernumber:ordernumber,
          orderPrice:orderPrice,
          dayTime:dayTime
        }
      })
      cart.forEach(v=>{
        let goods_id=v.goods_id
        let saleNum=v.saleNum+v.num
        db.collection('goodsDetail').where({
            goods_id:goods_id,
        })
        .update({
          data:{
            saleNum:parseInt(saleNum)
          },
          success(res) {
            console.log(res);
          }
        })
      })
      setTimeout(function() {
        wx.reLaunch({
          url: '/pages/order/order',
        })
      },2000)
     }
   })
 }
})